package com.itcodai.design_pattern.behavioral.strategy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author c-fanqj
 * @date 2023/3/17
 * @apiNote
 */
@Service
public class DisCountStrageService {

    Map<String, DiscountStrategy> discountStrategyMap = new HashMap<>();

    // 构造函数，如果你是集合接口对象，会把spring容器中所有关于该接口的子类，全部抓出来放入到集合中
    @Autowired
    public DisCountStrageService(List<DiscountStrategy> discountStrategys) {
        for (DiscountStrategy discountStrategy : discountStrategys) {
            discountStrategyMap.put(discountStrategy.getType(), discountStrategy);
        }
    }

    public double disCount(String type, Double fee) {
        DiscountStrategy discountStrategy = discountStrategyMap.get(type);
        return discountStrategy.disCount(fee);
    }
}
